// AWTCheckbox.cpp: implementation of the AWTCheckbox class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "AWTCheckbox.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

IMPLEMENT_DYNAMIC(AWTCheckbox, AWTComponent)

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

AWTCheckbox::AWTCheckbox()
{
	construct(CString(""), FALSE, NULL);
}

AWTCheckbox::AWTCheckbox(LPTSTR label)
{
	construct(CString(label), FALSE, NULL);
}

AWTCheckbox::AWTCheckbox(LPTSTR label, BOOL state)
{
	construct(CString(label), state, NULL);
}

AWTCheckbox::AWTCheckbox(LPTSTR label, BOOL state, AWTCheckboxGroup* group)
{
	construct(CString(label), state, group);
}

AWTCheckbox::AWTCheckbox(LPTSTR label, AWTCheckboxGroup* group, BOOL state)
{
	construct(CString(label), state, group);
}

AWTCheckbox::AWTCheckbox(CString& label)
{
	construct(label, FALSE, NULL);
}

AWTCheckbox::AWTCheckbox(CString& label, BOOL state)
{
	construct(label, state, NULL);
}

AWTCheckbox::AWTCheckbox(CString& label, AWTCheckboxGroup* group, BOOL state)
{
	construct(label, state, group);
}


AWTCheckbox::~AWTCheckbox()
{
	if (m_group != NULL)
		m_group->Release();
	if (itemListener != NULL)
	{
		if (itemListener->isMultiCaster())
		{
#ifdef UNDER_CE
			AWTEventMulticaster* mc = (AWTEventMulticaster*)itemListener;
#else
			AWTEventMulticaster* mc = dynamic_cast<AWTEventMulticaster*>(itemListener);
#endif
			delete mc;
		}
	}
}

void AWTCheckbox::construct(CString& label, BOOL state, AWTCheckboxGroup* group) 
{
	itemListener = NULL;
	m_label = label;
	m_state = state;
	m_group = group;
	if (m_group != NULL)
	{
		m_group->AddRef();
		if (m_state) 
		{
			m_group->setSelectedCheckbox(this);
		}
	}
}

void AWTCheckbox::setStateInternal(BOOL state)
{
	m_state = state;
	if (m_peer != NULL) 
	{
		AWTCheckboxPeer* peer = (AWTCheckboxPeer*)m_peer;
	  peer->setState(state);
	}
}

void AWTCheckbox::setState(BOOL state) 
{
	AWTCheckboxGroup* group = m_group;
	if (group != NULL) 
	{
		if (state) 
		{
			group->setSelectedCheckbox(this);
		} 
		else if (group->getSelectedCheckbox() == this) 
		{
			state = TRUE;
	  }
	}
	setStateInternal(state);
}

CStringArray* AWTCheckbox::getSelectedObjects() 
{
/*
	if (m_state) 
	{
    CObArray* items = new CObArray;
    items->Add(&m_label);
    return items;
  }
*/
  return NULL;
}

void AWTCheckbox::setCheckboxGroup(AWTCheckboxGroup* g) 
{
  AWTCheckboxGroup* group = m_group;
	if (group != NULL) 
	{
	  group->setSelectedCheckbox(NULL);
	}
  m_group = g;
	AWTCheckboxPeer* peer = (AWTCheckboxPeer*)m_peer;
	if (peer != NULL) 
	{
		peer->setCheckboxGroup(g);
	}
}

void AWTCheckbox::addItemListener(AWTItemListener* l) 
{
  itemListener = AWTEventMulticaster::add(itemListener, l);
}

void AWTCheckbox::removeItemListener(AWTItemListener* l) 
{
  itemListener = AWTEventMulticaster::remove(itemListener, l);
}

void AWTCheckbox::processItemEvent(AWTItemEvent* e) 
{
  if (itemListener != NULL) 
	{
		itemListener->itemStateChanged(e);
  }
}

CWnd* AWTCheckbox::createComponent()					 
{
	CWnd* wParent = getParent()->getPeer();
	if (wParent == NULL)
		return wParent;
	CButton* pwnd = new AWTCheckboxPeer(this);
	if (m_group == NULL)
	{
		pwnd->Create(m_label, WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX, getBounds(), wParent, 0);
		if (m_state)
			pwnd->SetCheck(1);	
	}
	else
	{
		if (m_group->isFirst())
		{
			pwnd->Create(m_label, WS_CHILD | WS_VISIBLE | WS_GROUP| BS_AUTORADIOBUTTON, getBounds(), wParent, 0);
			m_group->setFirst(FALSE);
		}
		else
		{
			pwnd->Create(m_label, WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON, getBounds(), wParent, 0);
		}
		if (m_state)
		{
			pwnd->SetCheck(1);	
//			pwnd->SetFocus();
		}
	}
	pwnd->SetFont(CFont::FromHandle(m_font));
	pwnd->ShowWindow(SW_SHOWNA);
	if (hasFocus())
		pwnd->SetFocus();		
	if (!m_enabled)
		pwnd->EnableWindow(FALSE);
	return pwnd;
}

CSize AWTCheckbox::getMinimumSize()
{
	return getPreferredSize();
}

CSize AWTCheckbox::getPreferredSize()
{
	if (m_peer == NULL)
		return CSize(0,0);
	CButton* pwnd = (CButton*)m_peer;
	CString s;
	pwnd->GetWindowText(s);
	CDC* pdc = pwnd->GetDC();
	CFont* ft = CFont::FromHandle(m_font);
	CFont* ftOld = pdc->SelectObject(ft);
	CSize sz = pdc->GetTextExtent(s);
	pdc->SelectObject(ftOld);
	pwnd->ReleaseDC(pdc);	
	sz.cx += 30;
	sz.cy += 8;
	return sz;
}



